package com.geekteam.manga.provider;

import java.util.LinkedList;
import java.util.List;

import com.geekteam.manga.Model.ContentMangaModel;
import com.geekteam.manga.Model.MangaChapModel;
import com.geekteam.manga.Model.MangaModel;
import com.geekteam.manga.Model.Mangas;
import com.geekteam.manga.Model.RecentMangaModel;
import com.geekteam.manga.provider.MangaContract.ContentManga;
import com.geekteam.manga.provider.MangaContract.Manga;
import com.geekteam.manga.provider.MangaContract.MangaChap;
import com.geekteam.manga.provider.MangaContract.RecentManga;
import com.geekteam.manga.provider.MangaDatabase.Tables;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;


public class MangaAccessor {
	
	private MangaDatabase db;
	private SQLiteDatabase rdb;
	private SQLiteDatabase rwdb;
	
	public MangaAccessor(Context mContext){
		db = new MangaDatabase(mContext);
		rdb = db.getReadableDatabase();
		rwdb = db.getWritableDatabase();
	}
	
	public void onCloseConnect(){
		rdb.close();
		rwdb.close();
	}
	
	//====================================================Recent Manga=====================================================

	public List<Long> getAllRecentManga(){
		LinkedList<Long> recentMangas = new LinkedList<Long>();
		Cursor cursor = rdb.query(Tables.RECENTMANGA, new String[]{RecentManga._ID, RecentManga.RECENTMANGA_TIME}, null, 
				null, null, null, RecentManga.RECENTMANGA_TIME + " DESC");
		
		while(cursor.moveToNext()){
			Long recentMangaId = cursor.getLong(cursor.getColumnIndex(RecentManga._ID));
			recentMangas.add(recentMangaId);
		}
		return recentMangas;
	}
	
	public RecentMangaModel getRecentManga(Long id){
		
		Cursor cursor = rdb.query(Tables.RECENTMANGA, RecentMangaModel.contentColumns(), RecentManga._ID+"="+id, 
				null, null, null, null);
		
		if (cursor.getCount() != 1) {
			cursor.close();
			return null;
		}
		
		cursor.moveToFirst();
		RecentMangaModel model = new RecentMangaModel(cursor);
		cursor.close();
		return model;
		
	}
	
	public boolean WriteRecentManga(long id, RecentMangaModel model){
		Cursor cursor = rdb.query(Tables.RECENTMANGA, RecentMangaModel.contentColumns(), RecentManga._ID+"="+id, 
				null, null, null, null);
		
		boolean success = false;
		
		if(cursor.getCount()<1){
			success = rwdb.insert(Tables.RECENTMANGA, null, model.contentValues(id)) >= 0;
		}else{
			success = rwdb.update(Tables.RECENTMANGA,  model.contentValues(id),RecentManga._ID + "=" + id, null) == 1;
		}
		cursor.close();
		return success;
		
	}
	
	//==================================================Manga Chap====================================================
	public List<Long> getAllMangaChap(){
		LinkedList<Long> mangaChap = new LinkedList<Long>();
		Cursor cursor = rdb.query(Tables.MANGACHAP, new String[]{MangaChap._ID, MangaChap.MANGA_ID, MangaChap.MANGACHAP_CHAPID,MangaChap.MANGACHAP_NUMBER}, null, 
				null, null, null, MangaChap.MANGACHAP_NUMBER + " DESC");
		
		while(cursor.moveToNext()){
			Long MangaChapId = cursor.getLong(cursor.getColumnIndex(MangaChap._ID));
			mangaChap.add(MangaChapId);
		}
		return mangaChap;
	}
	
	public MangaChapModel getmangaChap(Long id){
		
		Cursor cursor = rdb.query(Tables.MANGACHAP, ContentMangaModel.contentColumns(), MangaChap._ID+"="+id, 
				null, null, null, null);
		
		if (cursor.getCount() != 1) {
			cursor.close();
			return null;
		}
		
		cursor.moveToFirst();
		MangaChapModel model = new MangaChapModel(cursor);
		cursor.close();
		return model;
		
	}
	
	public boolean WriteMangaChap(long id, MangaChapModel model){
		Cursor cursor = rdb.query(Tables.MANGACHAP, MangaChapModel.contentColumns(), MangaChap._ID+"="+id, 
				null, null, null, null);
		
		boolean success = false;
		
		if(cursor.getCount()<1){
			success = rwdb.insert(Tables.MANGACHAP, null, model.contentValues(id)) >= 0;
		}else{
			success = rwdb.update(Tables.MANGACHAP,  model.contentValues(id), MangaChap._ID + "=" + id, null) == 1;
		}
		cursor.close();
		return success;
		
	}
	
	//================================================Content Chap=================================================
	
	public List<Long> getAllContentManga(){
		LinkedList<Long> contentManga = new LinkedList<Long>();
		Cursor cursor = rdb.query(Tables.CONTENTMANGA, new String[]{ContentManga._ID, ContentManga.MANGA_ID, ContentManga.MANGA_CHAPID,ContentManga.MANGA_CONTENT}, null, 
				null, null, null, ContentManga.MANGA_CHAPID + " DESC");
		
		while(cursor.moveToNext()){
			Long ContentMangaId = cursor.getLong(cursor.getColumnIndex(ContentManga._ID));
			contentManga.add(ContentMangaId);
		}
		return contentManga;
	}
	
	public ContentMangaModel getContentManga(Long id){
		
		Cursor cursor = rdb.query(Tables.CONTENTMANGA, ContentMangaModel.contentColumns(), ContentManga._ID+"="+id, 
				null, null, null, null);
		
		if (cursor.getCount() != 1) {
			cursor.close();
			return null;
		}
		
		cursor.moveToFirst();
		ContentMangaModel model = new ContentMangaModel(cursor);
		cursor.close();
		return model;
		
	}
	
	public boolean WriteContentManga(long id, ContentMangaModel model){
		Cursor cursor = rdb.query(Tables.CONTENTMANGA, ContentMangaModel.contentColumns(), ContentManga._ID+"="+id, 
				null, null, null, null);
		
		boolean success = false;
		
		if(cursor.getCount()<1){
			success = rwdb.insert(Tables.CONTENTMANGA, null, model.contentValues(id)) >= 0;
		}else{
			success = rwdb.update(Tables.CONTENTMANGA,  model.contentValues(id), ContentManga._ID + "=" + id, null) == 1;
		}
		cursor.close();
		return success;
		
	}
	
	//======================================================Manga=======================================================
	
	public List<Long> getAllManga(boolean isFavorite){
		LinkedList<Long> manga = new LinkedList<Long>();
		Cursor cursor = rdb.query(Tables.MANGA, new String[] { Manga._ID },
				isFavorite ? (Manga.MANGA_FAVORITE + "=" + 1) : null, null, null,
				null, null);
		
		while (cursor.moveToNext()) {
			long MangaId = cursor.getLong(cursor.getColumnIndex(Manga._ID));
			manga.add(MangaId);
		}
		return manga;
	}
	
	public MangaModel getManga(long id) {
		Cursor cursor = rdb.query(Tables.MANGA, MangaModel.contentColumns(),
				Manga._ID + " = " + id, null, null, null, null);
		if (cursor.getCount() != 1) {
			cursor.close();
			return null;
		}

		cursor.moveToFirst();
		MangaModel model = new MangaModel(cursor);
		cursor.close();
		return model;
	}
	
	public boolean writeManga(long id, MangaModel model) {
		Cursor cursor = rdb.query(Tables.MANGA, new String[] { Manga._ID }, Manga._ID
				+ " = " + id, null, null, null, null);
		boolean success = false;
		if (cursor.getCount() < 1) {
			success = rwdb.insert(Tables.MANGA, null, model.contentValues(id)) >= 0;
		} else {
			success = rwdb.update(Tables.MANGA, model.contentValues(id), Manga._ID
					+ "=" + id, null) == 1;
		}

		cursor.close();
		return success;
	}
	
	public void writeJob(Mangas mangas){
		if(mangas!=null){
			if(mangas.code==200 && mangas.listJob!=null){
				for(MangaModel model : mangas.listJob){
					rwdb.insert(Tables.MANGA, null, model.contentValues());
				}
			}
		}
	}
	
	public boolean deleteManga(long id) {
		int count = rwdb.delete(Tables.MANGA, Manga._ID + "=" + id, null);
		return count > 0;
	}

	public boolean deleteAllMangaNotFavorite() {
		int count = rwdb.delete(Tables.MANGA, Manga.MANGA_FAVORITE + "= 0", null);
		return count > 0;
	}

	public boolean deleteAllManga() {
		int count = rwdb.delete(Tables.MANGA, null, null);
		return count > 0;
	}
	
}
